<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>palette.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_palette.js.html">palette.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A palette control.  A palette is a grid that the user can
<a name="line17"></a> * highlight or select via the keyboard or the mouse.
<a name="line18"></a> *
<a name="line19"></a> * @author attila@google.com (Attila Bodis)
<a name="line20"></a> * @see ../demos/palette.html
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.ui.Palette&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line28"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line29"></a>goog.require(&#39;goog.math.Size&#39;);
<a name="line30"></a>goog.require(&#39;goog.ui.Component&#39;);
<a name="line31"></a>goog.require(&#39;goog.ui.Control&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.PaletteRenderer&#39;);
<a name="line33"></a>goog.require(&#39;goog.ui.SelectionModel&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * A palette is a grid of DOM nodes that the user can highlight or select via
<a name="line39"></a> * the keyboard or the mouse.  The selection state of the palette is controlled
<a name="line40"></a> * an ACTION event.  Event listeners may retrieve the selected item using the
<a name="line41"></a> * {@link #getSelectedItem} or {@link #getSelectedIndex} method.
<a name="line42"></a> *
<a name="line43"></a> * Use this class as the base for components like color palettes or emoticon
<a name="line44"></a> * pickers.  Use {@link #setContent} to set/change the items in the palette
<a name="line45"></a> * after construction.  See palette.html demo for example usage.
<a name="line46"></a> *
<a name="line47"></a> * @param {Array.&lt;Node&gt;} items Array of DOM nodes to be displayed as items
<a name="line48"></a> *     in the palette grid (limited to one per cell).
<a name="line49"></a> * @param {goog.ui.PaletteRenderer=} opt_renderer Renderer used to render or
<a name="line50"></a> *     decorate the palette; defaults to {@link goog.ui.PaletteRenderer}.
<a name="line51"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
<a name="line52"></a> *     document interaction.
<a name="line53"></a> * @constructor
<a name="line54"></a> * @extends {goog.ui.Control}
<a name="line55"></a> */
<a name="line56"></a>goog.ui.Palette = function(items, opt_renderer, opt_domHelper) {
<a name="line57"></a>  goog.base(this, items,
<a name="line58"></a>      opt_renderer || goog.ui.PaletteRenderer.getInstance(), opt_domHelper);
<a name="line59"></a>  this.setAutoStates(goog.ui.Component.State.CHECKED |
<a name="line60"></a>      goog.ui.Component.State.SELECTED | goog.ui.Component.State.OPENED, false);
<a name="line61"></a>};
<a name="line62"></a>goog.inherits(goog.ui.Palette, goog.ui.Control);
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * Events fired by the palette object
<a name="line67"></a> * @enum {string}
<a name="line68"></a> */
<a name="line69"></a>goog.ui.Palette.EventType = {
<a name="line70"></a>  AFTER_HIGHLIGHT: goog.events.getUniqueId(&#39;afterhighlight&#39;)
<a name="line71"></a>};
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Palette dimensions (columns x rows).  If the number of rows is undefined,
<a name="line76"></a> * it is calculated on first use.
<a name="line77"></a> * @type {goog.math.Size}
<a name="line78"></a> * @private
<a name="line79"></a> */
<a name="line80"></a>goog.ui.Palette.prototype.size_ = null;
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * Index of the currently highlighted item (-1 if none).
<a name="line85"></a> * @type {number}
<a name="line86"></a> * @private
<a name="line87"></a> */
<a name="line88"></a>goog.ui.Palette.prototype.highlightedIndex_ = -1;
<a name="line89"></a>
<a name="line90"></a>
<a name="line91"></a>/**
<a name="line92"></a> * Selection model controlling the palette&#39;s selection state.
<a name="line93"></a> * @type {goog.ui.SelectionModel}
<a name="line94"></a> * @private
<a name="line95"></a> */
<a name="line96"></a>goog.ui.Palette.prototype.selectionModel_ = null;
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>// goog.ui.Component / goog.ui.Control implementation.
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/** @override */
<a name="line103"></a>goog.ui.Palette.prototype.disposeInternal = function() {
<a name="line104"></a>  goog.ui.Palette.superClass_.disposeInternal.call(this);
<a name="line105"></a>
<a name="line106"></a>  if (this.selectionModel_) {
<a name="line107"></a>    this.selectionModel_.dispose();
<a name="line108"></a>    this.selectionModel_ = null;
<a name="line109"></a>  }
<a name="line110"></a>
<a name="line111"></a>  this.size_ = null;
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Overrides {@link goog.ui.Control#setContentInternal} by also updating the
<a name="line117"></a> * grid size and the selection model.  Considered protected.
<a name="line118"></a> * @param {goog.ui.ControlContent} content Array of DOM nodes to be displayed
<a name="line119"></a> *     as items in the palette grid (one item per cell).
<a name="line120"></a> * @protected
<a name="line121"></a> * @override
<a name="line122"></a> */
<a name="line123"></a>goog.ui.Palette.prototype.setContentInternal = function(content) {
<a name="line124"></a>  var items = /** @type {Array.&lt;Node&gt;} */ (content);
<a name="line125"></a>  goog.ui.Palette.superClass_.setContentInternal.call(this, items);
<a name="line126"></a>
<a name="line127"></a>  // Adjust the palette size.
<a name="line128"></a>  this.adjustSize_();
<a name="line129"></a>
<a name="line130"></a>  // Add the items to the selection model, replacing previous items (if any).
<a name="line131"></a>  if (this.selectionModel_) {
<a name="line132"></a>    // We already have a selection model; just replace the items.
<a name="line133"></a>    this.selectionModel_.clear();
<a name="line134"></a>    this.selectionModel_.addItems(items);
<a name="line135"></a>  } else {
<a name="line136"></a>    // Create a selection model, initialize the items, and hook up handlers.
<a name="line137"></a>    this.selectionModel_ = new goog.ui.SelectionModel(items);
<a name="line138"></a>    this.selectionModel_.setSelectionHandler(goog.bind(this.selectItem_,
<a name="line139"></a>        this));
<a name="line140"></a>    this.getHandler().listen(this.selectionModel_,
<a name="line141"></a>        goog.events.EventType.SELECT, this.handleSelectionChange);
<a name="line142"></a>  }
<a name="line143"></a>
<a name="line144"></a>  // In all cases, clear the highlight.
<a name="line145"></a>  this.highlightedIndex_ = -1;
<a name="line146"></a>};
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * Overrides {@link goog.ui.Control#getCaption} to return the empty string,
<a name="line151"></a> * since palettes don&#39;t have text captions.
<a name="line152"></a> * @return {string} The empty string.
<a name="line153"></a> * @override
<a name="line154"></a> */
<a name="line155"></a>goog.ui.Palette.prototype.getCaption = function() {
<a name="line156"></a>  return &#39;&#39;;
<a name="line157"></a>};
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * Overrides {@link goog.ui.Control#setCaption} to be a no-op, since palettes
<a name="line162"></a> * don&#39;t have text captions.
<a name="line163"></a> * @param {string} caption Ignored.
<a name="line164"></a> * @override
<a name="line165"></a> */
<a name="line166"></a>goog.ui.Palette.prototype.setCaption = function(caption) {
<a name="line167"></a>  // Do nothing.
<a name="line168"></a>};
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>// Palette event handling.
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/**
<a name="line175"></a> * Handles mouseover events.  Overrides {@link goog.ui.Control#handleMouseOver}
<a name="line176"></a> * by determining which palette item (if any) was moused over, highlighting it,
<a name="line177"></a> * and un-highlighting any previously-highlighted item.
<a name="line178"></a> * @param {goog.events.BrowserEvent} e Mouse event to handle.
<a name="line179"></a> * @override
<a name="line180"></a> */
<a name="line181"></a>goog.ui.Palette.prototype.handleMouseOver = function(e) {
<a name="line182"></a>  goog.ui.Palette.superClass_.handleMouseOver.call(this, e);
<a name="line183"></a>
<a name="line184"></a>  var item = this.getRenderer().getContainingItem(this, e.target);
<a name="line185"></a>  if (item &amp;&amp; e.relatedTarget &amp;&amp; goog.dom.contains(item, e.relatedTarget)) {
<a name="line186"></a>    // Ignore internal mouse moves.
<a name="line187"></a>    return;
<a name="line188"></a>  }
<a name="line189"></a>
<a name="line190"></a>  if (item != this.getHighlightedItem()) {
<a name="line191"></a>    this.setHighlightedItem(item);
<a name="line192"></a>  }
<a name="line193"></a>};
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/**
<a name="line197"></a> * Handles mouseout events.  Overrides {@link goog.ui.Control#handleMouseOut}
<a name="line198"></a> * by determining the palette item that the mouse just left (if any), and
<a name="line199"></a> * making sure it is un-highlighted.
<a name="line200"></a> * @param {goog.events.BrowserEvent} e Mouse event to handle.
<a name="line201"></a> * @override
<a name="line202"></a> */
<a name="line203"></a>goog.ui.Palette.prototype.handleMouseOut = function(e) {
<a name="line204"></a>  goog.ui.Palette.superClass_.handleMouseOut.call(this, e);
<a name="line205"></a>
<a name="line206"></a>  var item = this.getRenderer().getContainingItem(this, e.target);
<a name="line207"></a>  if (item &amp;&amp; e.relatedTarget &amp;&amp; goog.dom.contains(item, e.relatedTarget)) {
<a name="line208"></a>    // Ignore internal mouse moves.
<a name="line209"></a>    return;
<a name="line210"></a>  }
<a name="line211"></a>
<a name="line212"></a>  if (item == this.getHighlightedItem()) {
<a name="line213"></a>    this.getRenderer().highlightCell(this, item, false);
<a name="line214"></a>  }
<a name="line215"></a>};
<a name="line216"></a>
<a name="line217"></a>
<a name="line218"></a>/**
<a name="line219"></a> * Handles mousedown events.  Overrides {@link goog.ui.Control#handleMouseDown}
<a name="line220"></a> * by ensuring that the item on which the user moused down is highlighted.
<a name="line221"></a> * @param {goog.events.Event} e Mouse event to handle.
<a name="line222"></a> * @override
<a name="line223"></a> */
<a name="line224"></a>goog.ui.Palette.prototype.handleMouseDown = function(e) {
<a name="line225"></a>  goog.ui.Palette.superClass_.handleMouseDown.call(this, e);
<a name="line226"></a>
<a name="line227"></a>  if (this.isActive()) {
<a name="line228"></a>    // Make sure we move the highlight to the cell on which the user moused
<a name="line229"></a>    // down.
<a name="line230"></a>    var item = this.getRenderer().getContainingItem(this, e.target);
<a name="line231"></a>    if (item != this.getHighlightedItem()) {
<a name="line232"></a>      this.setHighlightedItem(item);
<a name="line233"></a>    }
<a name="line234"></a>  }
<a name="line235"></a>};
<a name="line236"></a>
<a name="line237"></a>
<a name="line238"></a>/**
<a name="line239"></a> * Selects the currently highlighted palette item (triggered by mouseup or by
<a name="line240"></a> * keyboard action).  Overrides {@link goog.ui.Control#performActionInternal}
<a name="line241"></a> * by selecting the highlighted item and dispatching an ACTION event.
<a name="line242"></a> * @param {goog.events.Event} e Mouse or key event that triggered the action.
<a name="line243"></a> * @return {boolean} True if the action was allowed to proceed, false otherwise.
<a name="line244"></a> * @override
<a name="line245"></a> */
<a name="line246"></a>goog.ui.Palette.prototype.performActionInternal = function(e) {
<a name="line247"></a>  var item = this.getHighlightedItem();
<a name="line248"></a>  if (item) {
<a name="line249"></a>    this.setSelectedItem(item);
<a name="line250"></a>    return goog.base(this, &#39;performActionInternal&#39;, e);
<a name="line251"></a>  }
<a name="line252"></a>  return false;
<a name="line253"></a>};
<a name="line254"></a>
<a name="line255"></a>
<a name="line256"></a>/**
<a name="line257"></a> * Handles keyboard events dispatched while the palette has focus.  Moves the
<a name="line258"></a> * highlight on arrow keys, and selects the highlighted item on Enter or Space.
<a name="line259"></a> * Returns true if the event was handled, false otherwise.  In particular, if
<a name="line260"></a> * the user attempts to navigate out of the grid, the highlight isn&#39;t changed,
<a name="line261"></a> * and this method returns false; it is then up to the parent component to
<a name="line262"></a> * handle the event (e.g. by wrapping the highlight around).  Overrides {@link
<a name="line263"></a> * goog.ui.Control#handleKeyEvent}.
<a name="line264"></a> * @param {goog.events.KeyEvent} e Key event to handle.
<a name="line265"></a> * @return {boolean} True iff the key event was handled by the component.
<a name="line266"></a> * @override
<a name="line267"></a> */
<a name="line268"></a>goog.ui.Palette.prototype.handleKeyEvent = function(e) {
<a name="line269"></a>  var items = this.getContent();
<a name="line270"></a>  var numItems = items ? items.length : 0;
<a name="line271"></a>  var numColumns = this.size_.width;
<a name="line272"></a>
<a name="line273"></a>  // If the component is disabled or the palette is empty, bail.
<a name="line274"></a>  if (numItems == 0 || !this.isEnabled()) {
<a name="line275"></a>    return false;
<a name="line276"></a>  }
<a name="line277"></a>
<a name="line278"></a>  // User hit ENTER or SPACE; trigger action.
<a name="line279"></a>  if (e.keyCode == goog.events.KeyCodes.ENTER ||
<a name="line280"></a>      e.keyCode == goog.events.KeyCodes.SPACE) {
<a name="line281"></a>    return this.performActionInternal(e);
<a name="line282"></a>  }
<a name="line283"></a>
<a name="line284"></a>  // User hit HOME or END; move highlight.
<a name="line285"></a>  if (e.keyCode == goog.events.KeyCodes.HOME) {
<a name="line286"></a>    this.setHighlightedIndex(0);
<a name="line287"></a>    return true;
<a name="line288"></a>  } else if (e.keyCode == goog.events.KeyCodes.END) {
<a name="line289"></a>    this.setHighlightedIndex(numItems - 1);
<a name="line290"></a>    return true;
<a name="line291"></a>  }
<a name="line292"></a>
<a name="line293"></a>  // If nothing is highlighted, start from the selected index.  If nothing is
<a name="line294"></a>  // selected either, highlightedIndex is -1.
<a name="line295"></a>  var highlightedIndex = this.highlightedIndex_ &lt; 0 ? this.getSelectedIndex() :
<a name="line296"></a>      this.highlightedIndex_;
<a name="line297"></a>
<a name="line298"></a>  switch (e.keyCode) {
<a name="line299"></a>    case goog.events.KeyCodes.LEFT:
<a name="line300"></a>      if (highlightedIndex == -1) {
<a name="line301"></a>        highlightedIndex = numItems;
<a name="line302"></a>      }
<a name="line303"></a>      if (highlightedIndex &gt; 0) {
<a name="line304"></a>        this.setHighlightedIndex(highlightedIndex - 1);
<a name="line305"></a>        e.preventDefault();
<a name="line306"></a>        return true;
<a name="line307"></a>      }
<a name="line308"></a>      break;
<a name="line309"></a>
<a name="line310"></a>    case goog.events.KeyCodes.RIGHT:
<a name="line311"></a>      if (highlightedIndex &lt; numItems - 1) {
<a name="line312"></a>        this.setHighlightedIndex(highlightedIndex + 1);
<a name="line313"></a>        e.preventDefault();
<a name="line314"></a>        return true;
<a name="line315"></a>      }
<a name="line316"></a>      break;
<a name="line317"></a>
<a name="line318"></a>    case goog.events.KeyCodes.UP:
<a name="line319"></a>      if (highlightedIndex == -1) {
<a name="line320"></a>        highlightedIndex = numItems + numColumns - 1;
<a name="line321"></a>      }
<a name="line322"></a>      if (highlightedIndex &gt;= numColumns) {
<a name="line323"></a>        this.setHighlightedIndex(highlightedIndex - numColumns);
<a name="line324"></a>        e.preventDefault();
<a name="line325"></a>        return true;
<a name="line326"></a>      }
<a name="line327"></a>      break;
<a name="line328"></a>
<a name="line329"></a>    case goog.events.KeyCodes.DOWN:
<a name="line330"></a>      if (highlightedIndex == -1) {
<a name="line331"></a>        highlightedIndex = -numColumns;
<a name="line332"></a>      }
<a name="line333"></a>      if (highlightedIndex &lt; numItems - numColumns) {
<a name="line334"></a>        this.setHighlightedIndex(highlightedIndex + numColumns);
<a name="line335"></a>        e.preventDefault();
<a name="line336"></a>        return true;
<a name="line337"></a>      }
<a name="line338"></a>      break;
<a name="line339"></a>  }
<a name="line340"></a>
<a name="line341"></a>  return false;
<a name="line342"></a>};
<a name="line343"></a>
<a name="line344"></a>
<a name="line345"></a>/**
<a name="line346"></a> * Handles selection change events dispatched by the selection model.
<a name="line347"></a> * @param {goog.events.Event} e Selection event to handle.
<a name="line348"></a> */
<a name="line349"></a>goog.ui.Palette.prototype.handleSelectionChange = function(e) {
<a name="line350"></a>  // No-op in the base class.
<a name="line351"></a>};
<a name="line352"></a>
<a name="line353"></a>
<a name="line354"></a>// Palette management.
<a name="line355"></a>
<a name="line356"></a>
<a name="line357"></a>/**
<a name="line358"></a> * Returns the size of the palette grid.
<a name="line359"></a> * @return {goog.math.Size} Palette size (columns x rows).
<a name="line360"></a> */
<a name="line361"></a>goog.ui.Palette.prototype.getSize = function() {
<a name="line362"></a>  return this.size_;
<a name="line363"></a>};
<a name="line364"></a>
<a name="line365"></a>
<a name="line366"></a>/**
<a name="line367"></a> * Sets the size of the palette grid to the given size.  Callers can either
<a name="line368"></a> * pass a single {@link goog.math.Size} or a pair of numbers (first the number
<a name="line369"></a> * of columns, then the number of rows) to this method.  In both cases, the
<a name="line370"></a> * number of rows is optional and will be calculated automatically if needed.
<a name="line371"></a> * It is an error to attempt to change the size of the palette after it has
<a name="line372"></a> * been rendered.
<a name="line373"></a> * @param {goog.math.Size|number} size Either a size object or the number of
<a name="line374"></a> *     columns.
<a name="line375"></a> * @param {number=} opt_rows The number of rows (optional).
<a name="line376"></a> */
<a name="line377"></a>goog.ui.Palette.prototype.setSize = function(size, opt_rows) {
<a name="line378"></a>  if (this.getElement()) {
<a name="line379"></a>    throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
<a name="line380"></a>  }
<a name="line381"></a>
<a name="line382"></a>  this.size_ = goog.isNumber(size) ?
<a name="line383"></a>      new goog.math.Size(size, /** @type {number} */ (opt_rows)) : size;
<a name="line384"></a>
<a name="line385"></a>  // Adjust size, if needed.
<a name="line386"></a>  this.adjustSize_();
<a name="line387"></a>};
<a name="line388"></a>
<a name="line389"></a>
<a name="line390"></a>/**
<a name="line391"></a> * Returns the 0-based index of the currently highlighted palette item, or -1
<a name="line392"></a> * if no item is highlighted.
<a name="line393"></a> * @return {number} Index of the highlighted item (-1 if none).
<a name="line394"></a> */
<a name="line395"></a>goog.ui.Palette.prototype.getHighlightedIndex = function() {
<a name="line396"></a>  return this.highlightedIndex_;
<a name="line397"></a>};
<a name="line398"></a>
<a name="line399"></a>
<a name="line400"></a>/**
<a name="line401"></a> * Returns the currently highlighted palette item, or null if no item is
<a name="line402"></a> * highlighted.
<a name="line403"></a> * @return {Node} The highlighted item (undefined if none).
<a name="line404"></a> */
<a name="line405"></a>goog.ui.Palette.prototype.getHighlightedItem = function() {
<a name="line406"></a>  var items = this.getContent();
<a name="line407"></a>  return items &amp;&amp; items[this.highlightedIndex_];
<a name="line408"></a>};
<a name="line409"></a>
<a name="line410"></a>
<a name="line411"></a>/**
<a name="line412"></a> * Highlights the item at the given 0-based index, or removes the highlight
<a name="line413"></a> * if the argument is -1 or out of range.  Any previously-highlighted item
<a name="line414"></a> * will be un-highlighted.
<a name="line415"></a> * @param {number} index 0-based index of the item to highlight.
<a name="line416"></a> */
<a name="line417"></a>goog.ui.Palette.prototype.setHighlightedIndex = function(index) {
<a name="line418"></a>  if (index != this.highlightedIndex_) {
<a name="line419"></a>    this.highlightIndex_(this.highlightedIndex_, false);
<a name="line420"></a>    this.highlightedIndex_ = index;
<a name="line421"></a>    this.highlightIndex_(index, true);
<a name="line422"></a>    this.dispatchEvent(goog.ui.Palette.EventType.AFTER_HIGHLIGHT);
<a name="line423"></a>  }
<a name="line424"></a>};
<a name="line425"></a>
<a name="line426"></a>
<a name="line427"></a>/**
<a name="line428"></a> * Highlights the given item, or removes the highlight if the argument is null
<a name="line429"></a> * or invalid.  Any previously-highlighted item will be un-highlighted.
<a name="line430"></a> * @param {Node|undefined} item Item to highlight.
<a name="line431"></a> */
<a name="line432"></a>goog.ui.Palette.prototype.setHighlightedItem = function(item) {
<a name="line433"></a>  var items = /** @type {Array.&lt;Node&gt;} */ (this.getContent());
<a name="line434"></a>  this.setHighlightedIndex(items ? goog.array.indexOf(items, item) : -1);
<a name="line435"></a>};
<a name="line436"></a>
<a name="line437"></a>
<a name="line438"></a>/**
<a name="line439"></a> * Returns the 0-based index of the currently selected palette item, or -1
<a name="line440"></a> * if no item is selected.
<a name="line441"></a> * @return {number} Index of the selected item (-1 if none).
<a name="line442"></a> */
<a name="line443"></a>goog.ui.Palette.prototype.getSelectedIndex = function() {
<a name="line444"></a>  return this.selectionModel_ ? this.selectionModel_.getSelectedIndex() : -1;
<a name="line445"></a>};
<a name="line446"></a>
<a name="line447"></a>
<a name="line448"></a>/**
<a name="line449"></a> * Returns the currently selected palette item, or null if no item is selected.
<a name="line450"></a> * @return {Node} The selected item (null if none).
<a name="line451"></a> */
<a name="line452"></a>goog.ui.Palette.prototype.getSelectedItem = function() {
<a name="line453"></a>  return this.selectionModel_ ?
<a name="line454"></a>      /** @type {Node} */ (this.selectionModel_.getSelectedItem()) : null;
<a name="line455"></a>};
<a name="line456"></a>
<a name="line457"></a>
<a name="line458"></a>/**
<a name="line459"></a> * Selects the item at the given 0-based index, or clears the selection
<a name="line460"></a> * if the argument is -1 or out of range.  Any previously-selected item
<a name="line461"></a> * will be deselected.
<a name="line462"></a> * @param {number} index 0-based index of the item to select.
<a name="line463"></a> */
<a name="line464"></a>goog.ui.Palette.prototype.setSelectedIndex = function(index) {
<a name="line465"></a>  if (this.selectionModel_) {
<a name="line466"></a>    this.selectionModel_.setSelectedIndex(index);
<a name="line467"></a>  }
<a name="line468"></a>};
<a name="line469"></a>
<a name="line470"></a>
<a name="line471"></a>/**
<a name="line472"></a> * Selects the given item, or clears the selection if the argument is null or
<a name="line473"></a> * invalid.  Any previously-selected item will be deselected.
<a name="line474"></a> * @param {Node} item Item to select.
<a name="line475"></a> */
<a name="line476"></a>goog.ui.Palette.prototype.setSelectedItem = function(item) {
<a name="line477"></a>  if (this.selectionModel_) {
<a name="line478"></a>    this.selectionModel_.setSelectedItem(item);
<a name="line479"></a>  }
<a name="line480"></a>};
<a name="line481"></a>
<a name="line482"></a>
<a name="line483"></a>/**
<a name="line484"></a> * Private helper; highlights or un-highlights the item at the given index
<a name="line485"></a> * based on the value of the Boolean argument.  This implementation simply
<a name="line486"></a> * applies highlight styling to the cell containing the item to be highighted.
<a name="line487"></a> * Does nothing if the palette hasn&#39;t been rendered yet.
<a name="line488"></a> * @param {number} index 0-based index of item to highlight or un-highlight.
<a name="line489"></a> * @param {boolean} highlight If true, the item is highlighted; otherwise it
<a name="line490"></a> *     is un-highlighted.
<a name="line491"></a> * @private
<a name="line492"></a> */
<a name="line493"></a>goog.ui.Palette.prototype.highlightIndex_ = function(index, highlight) {
<a name="line494"></a>  if (this.getElement()) {
<a name="line495"></a>    var items = this.getContent();
<a name="line496"></a>    if (items &amp;&amp; index &gt;= 0 &amp;&amp; index &lt; items.length) {
<a name="line497"></a>      this.getRenderer().highlightCell(this, items[index], highlight);
<a name="line498"></a>    }
<a name="line499"></a>  }
<a name="line500"></a>};
<a name="line501"></a>
<a name="line502"></a>
<a name="line503"></a>/**
<a name="line504"></a> * Private helper; selects or deselects the given item based on the value of
<a name="line505"></a> * the Boolean argument.  This implementation simply applies selection styling
<a name="line506"></a> * to the cell containing the item to be selected.  Does nothing if the palette
<a name="line507"></a> * hasn&#39;t been rendered yet.
<a name="line508"></a> * @param {Node} item Item to select or deselect.
<a name="line509"></a> * @param {boolean} select If true, the item is selected; otherwise it is
<a name="line510"></a> *     deselected.
<a name="line511"></a> * @private
<a name="line512"></a> */
<a name="line513"></a>goog.ui.Palette.prototype.selectItem_ = function(item, select) {
<a name="line514"></a>  if (this.getElement()) {
<a name="line515"></a>    this.getRenderer().selectCell(this, item, select);
<a name="line516"></a>  }
<a name="line517"></a>};
<a name="line518"></a>
<a name="line519"></a>
<a name="line520"></a>/**
<a name="line521"></a> * Calculates and updates the size of the palette based on any preset values
<a name="line522"></a> * and the number of palette items.  If there is no preset size, sets the
<a name="line523"></a> * palette size to the smallest square big enough to contain all items.  If
<a name="line524"></a> * there is a preset number of columns, increases the number of rows to hold
<a name="line525"></a> * all items if needed.  (If there are too many rows, does nothing.)
<a name="line526"></a> * @private
<a name="line527"></a> */
<a name="line528"></a>goog.ui.Palette.prototype.adjustSize_ = function() {
<a name="line529"></a>  var items = this.getContent();
<a name="line530"></a>  if (items) {
<a name="line531"></a>    if (this.size_ &amp;&amp; this.size_.width) {
<a name="line532"></a>      // There is already a size set; honor the number of columns (if &gt;0), but
<a name="line533"></a>      // increase the number of rows if needed.
<a name="line534"></a>      var minRows = Math.ceil(items.length / this.size_.width);
<a name="line535"></a>      if (!goog.isNumber(this.size_.height) || this.size_.height &lt; minRows) {
<a name="line536"></a>        this.size_.height = minRows;
<a name="line537"></a>      }
<a name="line538"></a>    } else {
<a name="line539"></a>      // No size has been set; size the grid to the smallest square big enough
<a name="line540"></a>      // to hold all items (hey, why not?).
<a name="line541"></a>      var length = Math.ceil(Math.sqrt(items.length));
<a name="line542"></a>      this.size_ = new goog.math.Size(length, length);
<a name="line543"></a>    }
<a name="line544"></a>  } else {
<a name="line545"></a>    // No items; set size to 0x0.
<a name="line546"></a>    this.size_ = new goog.math.Size(0, 0);
<a name="line547"></a>  }
<a name="line548"></a>};
</pre>


</body>
</html>
